<?php
namespace api\controllers;

use Yii;
use yii\web\Controller;
use api\models\Ret;
use api\models\ApiLoginForm;
use yii\helpers\ArrayHelper;
use common\models\Account;
use common\models\Superstar;
use common\models\Article;
use common\models\Type;
use yii\filters\auth\QueryParamAuth;


class EchartController extends Controller
{
    public function behaviors(){
        return ArrayHelper::merge(parent::behaviors(), [
            'authenticatior' => [
                'class' => QueryParamAuth::className()
            ]
        ]); 
    }
    public function actionPie(){
      $token = Yii::$app->request->get("access-token");
      $accM = new Account();
      $ssid = $accM->getSurperByToken($token);
      $ssM = new Superstar();
      $res = $ssM->getFirstSuiperByAccount($ssid);
      $ret = new Ret(1,'获取成功',$res);
      $ret->json_output();
    }
    public function actionFrom()
    {
        $token = Yii::$app->request->get("access-token");
        $nature = Yii::$app->request->get("nature");
        if($nature == 1){
            $where = ['<','atcl_negative_prob',0.25];
        }elseif($nature == 0){
            $where = ['between','atcl_negative_prob',0.25,0.75];
        }else{
            $where = ['>','atcl_negative_prob',0.75];
        }
        $accM = new Account();
        $ssid = $accM->getSurperByToken($token);

        $type = Type::find()->select(['type_id as id','type_name as name'])->asArray()->all();
        $atcl = new Article();
        $sum = $atcl->find()->where(['and',['=','ss_id',$ssid],$where])->count();
        for($i=0;$i<count($type);$i++){
            $typeCount = $atcl->find()->where(['and',['=','ss_id',$ssid],['and',$where,['=','type_id',$type[$i]['id']]]])->count();
            $type[$i]['proportion']=$typeCount/$sum;
        }  
        $ret = new Ret(1,'获取成功',$type);
        $ret->json_output();
    }
}
